package com.ideal.manage.esms.controller.project;

import com.ideal.manage.esms.bean.DTO.Arithmetic;
import com.ideal.manage.esms.bean.DTO.DeployTree;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.bean.DTO.Result;
import com.ideal.manage.esms.bean.project.*;
import com.ideal.manage.esms.bean.sys.Parameter;
import com.ideal.manage.esms.service.project.*;
import com.ideal.manage.esms.service.system.ParameterService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 83730 on 2018/4/17.
 */
@Controller
@RequestMapping("project/arithmeticManage")
public class ArithmeticManageController {

    @Resource
    private ParameterService parameterService;
    @Resource
    private ArithmeticManageService arithmeticManageService;
    @Resource
    private ProjectService projectService;
    @Resource
    private EquipmentManageService equipmentManageService;
    @Resource
    private ProjectManageService projectManageService;
    @Resource
    private ParameterInfoService parameterInfoService;
    @Resource
    private AlarmInfoService alarmInfoService;
    @Resource
    private EquipmentInfoService equipmentInfoService;
    @Resource
    private AccountPropertyService accountPropertyService;

    /**
     * 算法管理-算法管理页面
     *
     * @param model
     */
    @RequestMapping("arithmeticManage_list")
    public void arithmeticManageList(Model model) {

    }

    /**
     * 算法管理-算法管理-算法导入
     *
     * @param model
     */
    @RequestMapping("arithmetic_import")
    public void arithmeticImport(Model model) {

    }

    /**
     * 算法管理-算法管理-新增
     *
     * @param model
     */
    @RequestMapping("manage_add")
    public void manageAdd(Model model) {
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        model.addAttribute("objects", objects);
    }

    /**
     * 算法管理-算法管理-编辑
     *
     * @param id
     * @param model
     */
    @RequestMapping("manage_edit")
    public void manageEdit(String id, Model model) {
        ArithmeticManage arithmeticManage = arithmeticManageService.findById(id);
        model.addAttribute("arithmeticManage", arithmeticManage);
        List<Parameter> objects = parameterService.findChildByParentCode("1010");
        model.addAttribute("objects", objects);
        model.addAttribute("id", id);
    }

    /**
     * 算法管理-算法管理页面-数据展示
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("manage_data")
    @ResponseBody
    public PageDto manageData(int pageNum, HttpServletRequest request) {
        Page<ArithmeticManage> page = arithmeticManageService.findAllManages(pageNum, request);
        List<ArithmeticManage> arithmeticManages = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(arithmeticManages);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法管理-保存算法
     *
     * @param id
     * @param name
     * @param objName
     * @param version
     * @param remark
     * @param inParameter
     * @param outParameter
     * @param isReverseControl
     * @param touchCondition
     * @param touchExpression
     * @param file
     * @param fileName
     * @return
     * @throws IOException
     */
    @RequestMapping("manage_save")
    @ResponseBody
    public Result manageSave(String id, String name, Integer objName, String version, String remark, String inParameter, String outParameter, String outParameterName,
                             Integer isReverseControl, String touchCondition, String touchExpression,
                             @RequestParam(value = "file", required = false) MultipartFile file, String fileName) throws IOException {
        arithmeticManageService.saveManage(id, name, objName, version, remark, inParameter, outParameter,outParameterName, isReverseControl, touchCondition, touchExpression, file, fileName);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 算法管理-算法管理-删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("manage_delete")
    @ResponseBody
    public Result manageDelete(String[] ids) {
        Result result = new Result();
        int count = 0;
        List<ArithmeticDeployManage> arithmeticDeployManages = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            arithmeticDeployManages = arithmeticManageService.findDeployById(ids[i]);
            if (!arithmeticDeployManages.isEmpty()) {
                count += 1;
            }
        }
        if (count != 0) {
            result.setType("alert");
            result.setMessage("该算法已使用，无法删除");
        } else {
            result.setType("alert");
            result.setMessage("删除成功");
            arithmeticManageService.delManage(ids);
        }
        return result;
    }

    /**
     * 算法管理-算法管理-新增-触发条件编辑
     *
     * @param model
     */
    @RequestMapping("condition_info")
    public void conditionInfo(Model model) {

    }

    /**
     * 算法管理-算法管理-新增-输入参数编辑
     *
     * @param model
     */
    @RequestMapping("inParameter_info")
    public void inParameterInfo(Model model) {

    }

    /**
     * 算法管理-算法管理-新增-输出参数编辑
     *
     * @param model
     */
    @RequestMapping("outParameter_info")
    public void outParameterInfo(Model model) {

    }

    /**
     * 算法管理-算法管理-新增-触发条件-数字输入
     *
     * @param model
     */
    @RequestMapping("condition_number")
    public void conditionNumber(Model model) {

    }

    /**
     * 算法管理-算法管理-新增-输入输出参数选择列表
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("Parameter_data")
    @ResponseBody
    public PageDto inParameterData(int pageNum, HttpServletRequest request) {
        Page<ParameterInfo> page = projectService.findAllParameterInfos(pageNum, request);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法管理-新增-触发条件-参数测点
     *
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("index_data")
    @ResponseBody
    public PageDto indexData(int pageNum, int pageSize, HttpServletRequest request) {
        Page<ParameterInfo> page = parameterInfoService.findAllParameterInfos(pageNum, request, pageSize);
        List<ParameterInfo> parameterInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(parameterInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法管理-新增-触发条件-过程告警
     *
     * @param pageNum
     * @param request
     * @param pageSize
     * @return
     */
    @RequestMapping("warn_data")
    @ResponseBody
    public PageDto warnData(int pageNum, HttpServletRequest request, int pageSize) {
        Page<AlarmInfo> page = alarmInfoService.findAllAlarmInfo(pageNum, request, pageSize);
        List<AlarmInfo> alarmInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(alarmInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法管理-新增-触发条件-设备状态
     *
     * @param pageNum
     * @param request
     * @param pageSize
     * @return
     */
    @RequestMapping("status_data")
    @ResponseBody
    public PageDto statusData(int pageNum, HttpServletRequest request, int pageSize) {
        Page<EquipmentInfo> page = equipmentInfoService.findAllEquipmentInfo(pageNum, request, pageSize);
        List<EquipmentInfo> equipmentInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(equipmentInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法管理-新增-触发条件-属性
     *
     * @param pageNum
     * @param request
     * @param pageSize
     * @return
     */
    @RequestMapping("property_data")
    @ResponseBody
    public PageDto propertyData(int pageNum, HttpServletRequest request, int pageSize) {
        Page<AccountProperty> page = accountPropertyService.findAllAccoutProperty(pageNum, request, pageSize);
        List<AccountProperty> accountPropertieInfos = page.getContent();
        long total = page.getTotalElements();
        PageDto pageDto = new PageDto();
        pageDto.setRows(accountPropertieInfos);
        pageDto.setTotal(total);
        return pageDto;
    }

    /**
     * 算法管理-算法部署页面
     *
     * @param model
     */
    @RequestMapping("arithmeticDeploy_list")
    public void arithmeticDeployList(Model model) {

    }

    /**
     * 算法管理-算法部署数据展示
     *
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping("deploy_data")
    @ResponseBody
    public PageDto deployData(int pageNum, HttpServletRequest request) {
        PageDto pageDto = arithmeticManageService.findAllDeployManages(pageNum, request);
        return pageDto;
    }

    /**
     * 算法管理-算法部署-新增
     *
     * @param model
     */
    @RequestMapping("deploy_add")
    public void deployAdd(Model model) {
        List<DeployTree> deployTrees = projectManageService.findDeployTree();
        deployTrees = equipmentManageService.findAllByProjectId(deployTrees);
        model.addAttribute("ztreeDtos", deployTrees);
        List<Arithmetic> arithmeticManages = arithmeticManageService.findArithmetics();
        model.addAttribute("arithmeticManages", arithmeticManages);
        List<ArithmeticDeployManage> arithmeticDeployManages = arithmeticManageService.findAllDeployManages();
        model.addAttribute("arithmeticDeployManages", arithmeticDeployManages);
        List<EquipmentManage> equipmentManages = equipmentManageService.findAllEquipments();
        model.addAttribute("equipmentManages", equipmentManages);
    }

    /**
     * 算法管理-算法部署-编辑
     *
     * @param model
     * @param id
     */
    @RequestMapping("deploy_edit")
    public void deployEdit(Model model, String id) {
        model.addAttribute("id", id);       //设备树的id
        List<DeployTree> deployTrees = projectManageService.findDeployTree(id);
        deployTrees = equipmentManageService.findTreeByProjectId(id, deployTrees);
        model.addAttribute("ztreeDtos", deployTrees);
        List<Arithmetic> arithmeticManages = arithmeticManageService.findArithmetics();
        model.addAttribute("arithmeticManages", arithmeticManages);
        List<ArithmeticDeployManage> arithmeticDeployManages = arithmeticManageService.findAllDeployManages();
        model.addAttribute("arithmeticDeployManages", arithmeticDeployManages);
        List<EquipmentManage> equipmentManages = equipmentManageService.findAllEquipments();
        model.addAttribute("equipmentManages", equipmentManages);
    }

    /**
     * 算法管理-算法部署-保存部署关系
     *
     * @param add
     * @param id
     * @return
     */
    @RequestMapping("save_arithmeticDeployManage")
    @ResponseBody
    public Result saveArithmeticDeployManage(String add, String id) {
        if (id != null) {
            //删除所选项目的所有关系
            arithmeticManageService.delDeploysById(id);
        } else {
            //先删除已有的所有关系
            arithmeticManageService.delAllDeploys();
        }
        // 1. 保存
        arithmeticManageService.saveArithmeticDeploy(add);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("保存成功");
        return result;
    }

    /**
     * 算法管理-算法部署-删除部署关系
     *
     * @param ids
     * @return
     */
    @RequestMapping("deploy_delete")
    @ResponseBody
    public Result deployDelete(String[] ids) {
        arithmeticManageService.delDeployManages(ids);
        Result result = new Result();
        result.setType("alert");
        result.setMessage("删除成功");
        return result;
    }

}
